package vn.keho.module.dompeto.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bingkaimedia.merchantbeta.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import vn.keho.define.convert.JsonUtil;
import vn.keho.module.dompeto.MerchantActivity;
import vn.keho.module.dompeto.model.ChannelModel;
import vn.keho.module.dompeto.model.ServiceModel;
import vn.keho.module.dompeto.rest.DompetoDefine;
import vn.keho.module.dompeto.rest.DompetoParam;
import vn.keho.module.dompeto.rest.DompetoRequest;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {link vn.keho.module.dompeto.fragment.LeftFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MerchantFragment extends Fragment {
    public static final int MERCHANT_FRAGMENT_ID = 0;

    private ListView listView;


    private ServiceListViewAdapter adapter;

    private List<ServiceModel> listData = new ArrayList<ServiceModel>();

    // TODO: Rename and change types and number of parameters
    public static MerchantFragment newInstance() {
        MerchantFragment fragment = new MerchantFragment();

        return fragment;
    }

    public MerchantFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new ServiceListViewAdapter();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_merchant, container, false);
        listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MerchantActivity merchantActivity = (MerchantActivity) getActivity();
                merchantActivity.onSelectService(listData.get(i));
            }
        });
        refreshData();
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    public void refreshData() {
        try {
            DompetoParam params = new DompetoParam(DompetoDefine.URL_MERCHANT_SERVICE_LIST);
            params.setUsername(DompetoRequest.username);
            params.setMappsidonapps(DompetoRequest.mappsidonapps);
            params.setToken(DompetoRequest.token);
            params.setChannelid(DompetoRequest.channelid);
            new DompetoRequest(getActivity(), new DompetoRequest.AsyncCallbackRequestHandler() {
                @Override
                public void handlerRequestSuccess(Object data) {
                    try {

                        listData.clear();
                        JSONArray jsonArray = new JSONArray(data.toString());
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        ChannelModel channelModel = JsonUtil.generateJavaObject(jsonObject.toString(), ChannelModel.class);
                        if(channelModel.getServicesize() > 0) {
                            listData.addAll(channelModel.getServices());
                        }
                        adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void handlerRequestError(String message) {
                    Log.i("Response", message);

                }
            }).executeStart(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class ServiceListViewAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.list_item_service_view, null);
            }

            final ServiceModel serviceModel = listData.get(position);

            TextView txtChannelTitle = (TextView) convertView.findViewById(R.id.txtServiceTitle);

            txtChannelTitle.setText(serviceModel.getServicetitle());

            return convertView;
        }
    }
}
